package cn.openea.perpartner.rule.web.controller;

import cn.openea.perpartner.common.auth.entity.LoginBody;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.rule.factory.UserContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author Weird077
 * @description TODO
 * @date 2022-09-22 9:48
 */
@Controller
public class IndexController extends BaseController {

    @RequestMapping("/")
    public String index(Model model, HttpServletRequest request) {
        LoginBody loginUser = UserContext.getLoginUser();
        model.addAttribute("loginUser", loginUser);
        model.addAttribute("Tab", "App/AppCenter");
        model.addAttribute("NAV", "应用中心");
        return "index :: MainPart";
    }

    @RequestMapping("/rule/preview")
    public String preview(Model model, HttpServletRequest request) {
        return "App/RulePreview";
    }

    @GetMapping("/tab/{flag}")
    public String route(Model model, @PathVariable String flag) {
        LoginBody loginUser = UserContext.getLoginUser();
        model.addAttribute("loginUser", loginUser);
        HashMap<String,String> map = getReturnValue(flag);
        model.addAttribute("Tab", map.get("Tab"));
        model.addAttribute("NAV", map.get("NAV"));
        //局部刷新的语法。前面是html页面，后面是th:fragment的值
        return "index :: MainPart";
    }

    @GetMapping("/tab/{flag}/{id}")
    public String detail(Model model, @PathVariable String flag, @PathVariable String id) {
        LoginBody loginUser = UserContext.getLoginUser();
        model.addAttribute("loginUser", loginUser);
        HashMap<String,String> map = getReturnValue(flag);
        model.addAttribute("Tab", map.get("Tab"));
        model.addAttribute("NAV", map.get("NAV"));
        //局部刷新的语法。前面是html页面，后面是th:fragment的值
        return "index :: MainPart";
    }

    private HashMap<String,String> getReturnValue(String flag){
        HashMap map = new HashMap<String,String>();
        if("App_RuleList".equals(flag)){
            map.put("Tab", "App/RuleList");
            map.put("NAV", "业务规则");
        }else if("App_RuleCode".equals(flag)){
            map.put("Tab", "App/RuleCode");
            map.put("NAV", "规则代码");
        }else{
            map.put("Tab", "App/AppCenter");
            map.put("NAV", "应用中心");
        }
        return map;
    }

}
